Skip to content

Disallow built-in internal types to be inferred by auto#8510

Open
joaosaffran wants to merge 9 commits into
microsoft:mainfrom
joaosaffran:auto/not-infer-builtin-types
Open

Disallow built-in internal types to be inferred by auto#8510
joaosaffran wants to merge 9 commits into
microsoft:mainfrom
joaosaffran:auto/not-infer-builtin-types

Conversation

@joaosaffran
Copy link
Copy Markdown
Collaborator

@joaosaffran joaosaffran commented Jun 1, 2026

This patch adds the check into SemaDecl to disallow auto to infer built-in internal types, like sample_type and mip_type. And Subobject types, like the ones used in DXR.

Copy link
Copy Markdown
Contributor

@tex3d tex3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback in comments, plus, can you try to have something in one of the tests to tease out each of the interesting cases in IsTypeDeducibleWithAuto? For example, is there a non-deducible component type in vector/matrix (I don't think it's possible)? Or how about the string case? Also, what about arrays of deducible/non-deducible types? Plus, what code hits the pointer case?

If some of these are unreachable (like pointer), perhaps they should use llvm_unreachable instead.

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
case AR_TOBJ_BASIC:
case AR_TOBJ_COMPOUND:
case AR_TOBJ_INTERFACE:
case AR_TOBJ_STRING:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure STRING should be included here.

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
// Container types: deducible iff their wrapped type is deducible.
case AR_TOBJ_MATRIX:
case AR_TOBJ_VECTOR:
return IsTypeDeducibleWithAuto(GetMatrixOrVectorElementType(type));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why matrix/vector types aren't just always deducible. What element type is allowed with these types (we have separate checking for the allowed element template type) that would prevent this?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

✅ With the latest revision this PR passed the C/C++ code formatter.

@joaosaffran joaosaffran requested a review from tex3d June 2, 2026 00:55
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated
if (IsSubobjectType(type))
return false;

switch (GetTypeObjectKind(type)) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only two cases here that actually matter are AR_TOBJ_INNER_OBJ and AR_TOBJ_STRING right?
Any other case should result in an error from somewhere else in the compiler.

Why not just handle the string case and put an attribute on the inner object types that marks them as non-deducible?

If we also put that attribute implicitly on subobject types, we can flatten a lot of this to just a hasAttr check.

@joaosaffran joaosaffran requested a review from llvm-beanz June 2, 2026 21:50
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Outdated

// Matrices.
float2x2 matInit = { 1, 2, 3, 4 };
auto m = matInit;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about something like?

    auto row = m[0];

joaosaffran and others added 2 commits June 2, 2026 17:46
Co-authored-by: Chris B <beanz@abolishcrlf.org>
@joaosaffran joaosaffran requested a review from llvm-beanz June 3, 2026 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

3 participants